package com.xiaolin.session.controller;

import com.xiaolin.session.model.Member;
import com.xiaolin.session.service.MemberService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName LoginController
 * @Description TODO
 * @Detail detail
 * @Author MyPC
 * @Date 2020/9/30
 * @Version 1.0
 */
@Controller
public class LoginController {

    @Autowired
    MemberService memberService;

    /**
     * 用户登录
     * @param member 只要参数名与对象属性对应上，表单数据会自动封装成对象
     * @param response
     * @param request @RequestParam("username") String username, @RequestParam("password") String password
     * @return
     * @throws IOException
     */
    @RequestMapping("/login")
    public void login(Member member, HttpServletResponse response, HttpServletRequest request) throws IOException {
        //设置请求参数编码是utf-8
        request.setCharacterEncoding("utf-8");
        // 上下文地址
        String contextPath = request.getContextPath();
        Map<String, Object> map = new HashMap<>();
        if(StringUtils.isEmpty(member.getUsername()) || StringUtils.isEmpty(member.getPassword())){
            map.put("msg","错误");
            response.sendRedirect(contextPath+"/error.html");
            return;
        }
        Member one = memberService.selectByName(member.getUsername());
        if(one == null || !one.getPassword().equals(member.getPassword())){
            map.put("msg","错误");
            response.sendRedirect(contextPath+"/error.html");
            return;
        }
        //将用户信息存储到session中
        HttpSession session = request.getSession();
        session.setAttribute("userInfo",one.getUsername());
        //设置session的过期时间，以秒为单位
        session.setMaxInactiveInterval(10);
        //将sessionId存入 cookie 中
        Cookie cookie=new Cookie("sessionId","userInfo");
        //设置cookie过期时间，以秒为单位
        cookie.setMaxAge(10);
        response.addCookie(cookie);
        response.sendRedirect(contextPath+"/index.html");
    }

    /**
     * 注销登录，即清除session的用户数据
     * @param request
     * @param response
     */
    @RequestMapping("/logout")
    public void logout(HttpServletRequest request,HttpServletResponse response){
        HttpSession session = request.getSession();
        session.removeAttribute("userInfo");
        try {
            response.setHeader("Cache-Control", "no-cache, must-revalidate");
            response.sendRedirect(request.getContextPath()+"/login.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/register")
    public void register(Member member,HttpServletRequest request,HttpServletResponse response) throws IOException {
        Member old = memberService.selectByName(member.getUsername());
        if(old!=null){
            //用户名已被注册
            response.sendRedirect(request.getContextPath()+"/register.html");
        }else {
            // 保存用户信息
            memberService.save(member);
            response.sendRedirect(request.getContextPath()+"/login.html");
        }
    }
}
